package com.css.fxfzypg.modules.baseData.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.constants.YNEnum;
import com.css.fxfzypg.modules.baseData.queryParams.SchoolQueryParams;
import com.css.fxfzypg.modules.baseData.repository.SchoolRepository;
import com.css.fxfzypg.modules.baseData.entity.SchoolEntity;
import com.css.fxfzypg.modules.baseData.service.SchooleService;
import com.css.fxfzypg.util.FileUtil;
import com.css.fxfzypg.util.PlatformDateUtils;
import com.css.fxfzypg.util.PlatformPageUtils;
import com.css.fxfzypg.web.PlatformSessionContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

/**
 * @author y1fan
 * @create 2022-08-15 17:19
 */
@Service
public class SchooleServiceImpl implements SchooleService {

    @Resource
    private SchoolRepository schoolRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SchoolEntity saveSchool(SchoolEntity schoolEntity) {
        String userID = PlatformSessionContext.getUserID();
        schoolEntity.setSchCreateUser(userID);
        schoolEntity.setSchCreatetime(PlatformDateUtils.getCurrentTimestamp());
        schoolEntity.setSchIsdelete(YNEnum.Y.toString());
        return schoolRepository.saveSchool(schoolEntity);
    }

    @Override
    public SchoolEntity findSchoolById(String dataSchoolId) {
        return schoolRepository.findSchoolById(dataSchoolId);
    }

    @Override
    public Map<String,Object> getAllSchoolByParams(SchoolQueryParams queryParams) {
        return schoolRepository.getAllSchoolByParams(queryParams);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteSchool(List<Integer> ids) {
        String userID = PlatformSessionContext.getUserID();
        Timestamp currentTimestamp = PlatformDateUtils.getCurrentTimestamp();

        schoolRepository.deleteSchool(ids,currentTimestamp,userID);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void exportExcel(HttpServletResponse response)throws IOException {
        List<SchoolEntity> list = schoolRepository.exportExcel();
        FileUtil.exportExcel(list, "学校数据", "sheet", SchoolEntity.class, "SchoolExcel", response);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importExcel(MultipartFile file)throws IOException {
        List<SchoolEntity> schoolEntities = FileUtil.importExcel(file, SchoolEntity.class);
        for (SchoolEntity schoolEntity : schoolEntities) {
            schoolEntity.setSchIsdelete(YNEnum.Y.toString());
            schoolEntity.setSchCreatetime(PlatformDateUtils.getCurrentTimestamp());

        }

        schoolRepository.importExcel(schoolEntities);
    }
}
